<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="ru" xml:lang="ru"> 
    <head>
	<title>LocalCRM</title>
	<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
	<script src="js/jquery-ui-1.8.4.custom.min.js" type="text/javascript"></script>
	<script src="js/jquery.blockUI.js" type="text/javascript"></script>
	<script src="js/jquery.contextMenu.js" type="text/javascript"></script>
	<script src="js/crm/common.js" type="text/javascript"></script>
	<link href="css/redmond/jquery-ui-1.8.4.custom.css" rel="stylesheet" type="text/css"/>
	<link href="css/jquery.contextMenu.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
	<script type="text/javascript">
	{literal}
	<!--
	$(document).ready
	(
	    function()
	    {
		$('.menu')
		    .find('li')
			.addClass('ui-widget')
			.css
			({
			    'font-size':	'10pt'
			})
			.addClass('ui-state-default')
			.hover
			(
			    function(){$(this).addClass('ui-state-highlight');},
			    function(){$(this).removeClass('ui-state-highlight');}
			);

		$('a#object-add').click
		(
		    function()
		    {
			$('#object-new')
			    .dialog('option','resizable',false)
			    .dialog('option','title',{/literal}'{$dictionaries[$table]}'{literal})
			    .dialog
			    (
				'option',
				'buttons',
				{
				    'Сохранить':	function()
							{
							    $('#object-new').find('form').submit();
							}
				}
			    )
			    .dialog('open');
		    }
		);

		$('.data-row')
		    .css('cursor','default')
		    .hover
		    (
			function(){$(this).css('background-color','#cdcdcd');},
			function(){$(this).css('background-color','transparent');}
		    )
		    .contextMenu
		    (
			{ menu:	'contextMenu-common' },
			function(action,el,pos)
			{
			    switch(action)
			    {
				case	'edit':
				    $.getJSON
				    (
					'dictionary.php',
					{
					    do:		'get_info',
					    table:	'{/literal}{$table}{literal}',
					    id:		$(el).children().html(),
					},
					function(json)
					{
					    if ( !json['object'] ) { alert('Ошибка получения объекта!'); return false; }

					    var tr = false;
					    for ( keyVar in json['object'] )
					    {
						tr += '<tr><td align="right">'+(json['titles'][keyVar]?json['titles'][keyVar]:keyVar)+'</td><td><textarea name="'+keyVar+'" '+((keyVar=='id'||keyVar=='options')?'readonly':false)+' style="width: 100%;">'+json['object'][keyVar]+'</textarea></td></tr>';
					    }

					    $('#object-edit').find('table').html(tr);

					    $('#object-edit')
						.dialog('option','resizable',false)
						.dialog('option','width',480)
						.dialog('option','title',{/literal}'{$dictionaries[$table]}'{literal})
						.dialog
						(
						    'option',
						    'buttons',
						    {
							'Сохранить':	function()
									{
									    $('#object-edit').find('form').submit();
									}
						    }
						)
						.dialog('open');
					}
				    )
				    break;

				case	'delete':
				    if(window.confirm('Вы уверены?'))
				    {
					$.getJSON
					(
					    'dictionary.php',
					    {
						do:	'delete',
						table:	'{/literal}{$table}{literal}',
						id:	$(el).children().html(),
					    },
					    function(json)
					    {
						switch(json['code'])
						{
						    case	200:
							window.location.reload();
							break;

						    case	403:
							alert('Невозможно удалить объект, т.к. в базе есть другие, зависимые от него, объекты!');
							break;

						    default:
							alert('Ошибка! '+json['code']);
							break;
						}
					    }
					);
				    }
				    break;
			    }
			}
		    );
	    }
	);
	-->
	{/literal}
	</script>

	{if $table!=''}
	<div class="dialog" id="object-new">
	    <form method="post">
	    <input type="hidden" name="table" value="{$table}"/>
	    <table>
	    {foreach from=$fields item=field}
		{if $field.Field!='id'}
		<tr>
		    <td align="right">{if $title[$table][$field.Field]!=''}{$title[$table][$field.Field]}{else}{$field.Field}{/if}</td>
		    <td><input type="text" name="{$field.Field}" value="{$field.Default|escape}"/></td>
		</tr>
		{/if}
	    {/foreach}
	    </table>
	    </form>
	</div>

	<div class="dialog" id="object-edit">
	    <form method="post">
	    <input type="hidden" name="table" value="{$table}"/>
	    <table width="100%">
	    </table>
	    </form>
	</div>

	<ul id="contextMenu-common" style="display: none;">
	    <li class="edit"><a href="#edit">Изменить</a></li>
	    <li class="delete"><a href="#delete">Удалить</a></li>
	</ul>
	{/if}

	<table width="100%">
	    <tr>
		<td valign="top" width="20%" style="border-right: solid 2px #000000;">
		    <ul class="menu">
		    {foreach from=$dictionaries item=dictionary key=key}
			<li {if $table==$key}class="ui-state-active"{/if}><a href="?table={$key}">{$dictionaries[$key]}</a></li>
		    {/foreach}
		    </ul>
		</td>
		<td valign="top">
		    {if $table!=''}
		    <table class="ui-widget" cellspacing="0">
			<thead class="ui-widget-header">
			{foreach from=$fields item=field}
			    <th>{if $title[$table][$field.Field]!=''}{$title[$table][$field.Field]}{else}{$field.Field}{/if}</th>
			{/foreach}
			</thead>
			<tbody>
			{foreach from=$items item=item}
			    <tr class="data-row">
			    {foreach from=$fields item=field}
				<td>{$item[$field.Field]}</td>
			    {/foreach}
			    </tr>
			{foreachelse}
			    <tr><td align="center" colspan="999"><i>Объектов не найдено</i></td></tr>
			{/foreach}
			</tbody>
		    </table>
		    <br/>
		    <a href="javascript:void(0)" id="object-add"><img src="images/icons/plus_16.png" border="0"/>&nbsp;Добавить объект</a>
		    {/if}
		</td>
	    </tr>
	</table>

    </body>
</html>
